package org.blogwu.learn.designpattern.chapter8.lazy.v2;

/**
 * 注重延迟加载
 */
public class LazySingleton {
    private LazySingleton() {

    }

    private static LazySingleton lazySingleton;

    public static LazySingleton getInstance() throws InterruptedException {
        //加锁
        synchronized (LazySingleton.class) {
            if (lazySingleton == null) {
                Thread.sleep(2000);
                lazySingleton = new LazySingleton();
            }
        }
        return lazySingleton;
    }


    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                System.out.println("t1:" + LazySingleton.getInstance());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                System.out.println("t2:" + LazySingleton.getInstance());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
    }

}
